package com.huizhonglm.diap.ui.wallet;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.wallet.RevenueInfo;
import com.huizhonglm.diap.api.model.wallet.RevenueInfoTotal;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.wallet.RcmRevenuePresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.wallet.iView.RcmRevenueIView;
import com.huizhonglm.diap.widget.LoadView;

import java.math.BigDecimal;
import java.util.List;

/**
 * 查询排队返现
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class RcmRevenueActivity extends BaseActivity implements RcmRevenueIView, LoadView.OnLoadAgainListener {

    private ListView mListView;
    private LoadView loadView;
    private TextView totalTxt;
    private TextView total_revenue_jl;
    private TextView total_revenue_fh;
    private TextView total_revenue_cyfh;
    private TextView total_revenue_jsfh;

    private String userID;
    private RcmRevenuePresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rcm_revenue);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        mPresenter = new RcmRevenuePresenter(this);
        assignViews();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        loadView = (LoadView) findViewById(R.id.load_view);
        mListView = (ListView) findViewById(R.id.revenue_list);
        totalTxt = (TextView) findViewById(R.id.total_revenue_txt);
        total_revenue_jl = (TextView) findViewById(R.id.total_revenue_jl);
        total_revenue_fh = (TextView) findViewById(R.id.total_revenue_fh);
        total_revenue_cyfh = (TextView) findViewById(R.id.total_revenue_cyfh);
        total_revenue_jsfh = (TextView) findViewById(R.id.total_revenue_jsfh);
        loadView.setDataView(mListView, this);
    }

    @Override
    public void onLoadAgain() {
        mPresenter.loadData(userID);
    }

    @Override
    public void onLoadDataSuccess(RevenueInfoTotal total) {
        if (total!=null && total.getProfitList()!=null && total.getProfitList().size()>0) {
            totalTxt.setText(formatDouble(total.getAllProfit(),2)+"");
            total_revenue_jl.setText(formatDouble(total.getRecommendedProfit(),2)+"");
            total_revenue_fh.setText(formatDouble(total.getBonusProfit(),2)+"");
            total_revenue_cyfh.setText(String.format("%d",total.getParticipateBonusCount()));
            total_revenue_jsfh.setText(String.format("%d",total.getEndBonusCount()));
            List<RevenueInfo> data=total.getProfitList();
            RcmRevenueAdapter adapter = new RcmRevenueAdapter(this, data);
            mListView.setAdapter(adapter);
            loadView.showDataView();
        } else {
            loadView.showEmptyView("当前无收益信息");
        }
    }
    /**
     * 格式化Double 小数位
     *
     * @Author StoneJxn
     * @Date 2016年12月28日 下午3:56:45
     * @param d
     * @param accuracy
     *            精度小数位
     * @return
     */
    public static double formatDouble(double d, int accuracy) {
        BigDecimal bg = new BigDecimal(d);
        double d1 = bg.setScale(accuracy, BigDecimal.ROUND_HALF_UP).doubleValue();
        return d1;
    }
    @Override
    public void onLoadDataFailure() {
        loadView.showErrorView();
    }
}
